package shell;

import java.util.ArrayList;
import java.util.List;

import util.DBUtil;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;

public class CRUD {

	public static void main(String[] args) throws Throwable {
		DB db = DBUtil.getDB("mydb");
		DBCollection collection = db.getCollection("crud");
		updateArrayPush(collection);
	}

	// insert
	private static void insert(DBCollection collection) {
		BasicDBObject doc = new BasicDBObject("title", "MongoDB")
				.append("description", "database").append("likes", 100)
				.append("url", "http://www.tutorialspoint.com/mongodb/")
				.append("by", "tutorials point");
		collection.insert(doc);

		// get id of of the newly inserted document
		System.out.println(doc.get("_id"));
	}

	private static void batchInsert(DBCollection collection) {
		List<DBObject> list = new ArrayList<DBObject>();
		DBObject doc = new BasicDBObject("title", "MongoDB 2")
				.append("description", "database").append("likes", 100)
				.append("url", "http://www.tutorialspoint.com/mongodb/")
				.append("by", "tutorials point");
		list.add(doc);
		collection.insert(list);
	}

	private static void batchInsertErrorsIgnored(DBCollection collection) {
		List<DBObject> list = new ArrayList<DBObject>();
		DBObject doc = new BasicDBObject("_id", 0);
		list.add(doc);
		doc = new BasicDBObject("_id", 1);
		list.add(doc);
		doc = new BasicDBObject("_id", 1); // should fail here. same key
		list.add(doc);
		doc = new BasicDBObject("_id", 2);
		list.add(doc);
		collection.insert(list, new WriteConcern(0, 0, false, false, true));
	}

	// delete
	private static void remove(DBCollection collection) {

		// removes all documents
		DBObject doc = new BasicDBObject(); // empty json
		collection.remove(doc);
	}

	// update
	private static void updateSet(DBCollection collection) {

		DBObject criteria = new BasicDBObject("weight", 704);
		BasicDBObject updatedDocument = new BasicDBObject();
		updatedDocument.append("$set", new BasicDBObject().append("vampires", 165));
		collection.update(criteria, updatedDocument);
	}
	
	private static void updateInc(DBCollection collection) {

		DBObject criteria = new BasicDBObject("weight", 650);
		BasicDBObject updatedDocument = new BasicDBObject();
		updatedDocument.append("$inc", new BasicDBObject().append("vampires", 1));
		collection.update(criteria, updatedDocument);
	}
	
	/**
	 * updates multiple documents who love apple.
	 * @param collection
	 */
	private static void updateMulti(DBCollection collection) {
		DBObject criteria = new BasicDBObject("loves", "apple");
		BasicDBObject updatedDocument = new BasicDBObject();
		updatedDocument.append("$set", new BasicDBObject().append("weight", 600));
		collection.updateMulti(criteria, updatedDocument);
	}
	
	/**
	 * 
	 * @param collection
	 */
	private static void updateUpsert(DBCollection collection) {
		DBObject criteria = new BasicDBObject("weight", 5000);
		BasicDBObject updatedDocument = new BasicDBObject();
		updatedDocument.append("$set", new BasicDBObject().append("vampires", 165));
		collection.update(criteria, updatedDocument,true,false); // upsert true
	}
	
	/**
	 * removes the field gender from the document. 
	 * @param collection
	 */
	private static void updateUnset(DBCollection collection) {
		DBObject criteria = new BasicDBObject("weight", 733);
		BasicDBObject updatedDocument = new BasicDBObject();
		updatedDocument.append("$set", new BasicDBObject().append("gender", "f"));
		collection.update(criteria, updatedDocument);
		
		updatedDocument = new BasicDBObject();
		updatedDocument.append("$unset", new BasicDBObject().append("gender", "f"));
		collection.update(criteria, updatedDocument);
	}
	
	private static void updateArrayPush(DBCollection collection) {
		DBObject criteria = new BasicDBObject("weight", 540);
		BasicDBObject updatedDocument = new BasicDBObject();
		updatedDocument.append("$push", new BasicDBObject().append("loves", "apple"));
		WriteResult update = collection.update(criteria, updatedDocument);
	}

	private static void updateAddToSet(DBCollection collection) {
		DBObject criteria = new BasicDBObject("weight", 540);
		BasicDBObject updatedDocument = new BasicDBObject();
		updatedDocument.append("$addToSet", new BasicDBObject().append("loves", "banana"));
		collection.update(criteria, updatedDocument);
	}
}
